<!--
 * @Author: zjj
 * @Date: 2019-10-29 12:56:59
 * @LastEditors: zjj
 * @LastEditTime: 2019-12-08 13:26:50
 -->
<!--  -->
<template>
  <div class=""></div>
</template>

<script>
import { parseCode } from "../api/api";
export default {
  components: {},
  data() {
    return {};
  },
  computed: {},
  watch: {},
  methods: {
    getUrlParams(names) {
      //获取？后面的参数
      var urlSearch = location.search;
      var urlValue = "";
      //以？*&来拆分
      var params = urlSearch.split(/[?*&]/);
      for (var i = 0; i < params.length; i++) {
        //如果url参数里包含传递过来names字段，则取=后面的部分
        if (params[i].indexOf(names) >= 0) {
          urlValue = params[i].split("=")[1];
          return urlValue;
        }
      }

      return urlValue;
    }
  },
  created() {},
  mounted() {
    let _state = decodeURIComponent(this.getUrlParams("state"));
    var stateArray = _state.split("|");
    let params = { code: this.getUrlParams("code") };
    if (stateArray[0] == "h5") {
      params = Object.assign({}, params, { pid: stateArray[1] });
    }
    if (stateArray[0] == "h5" && stateArray[3]) {
      params = Object.assign({}, params, { aid: stateArray[3] });
    }
    parseCode(params).then(res => {
      if (res.data.result.code == -1) {
        this.$store.commit("saveToken", res.data.result.token);
        this.$router.replace({
          path: "/quickLogin",
          query: {
            type: 1,
            pid: stateArray[1],
            aid: res.data.result.aid,
            wx_unionid: res.data.result.wx_unionid,
            openid:res.data.result.openid,
            user_headimg:res.data.result.user_headimg,
            user_name:res.data.result.user_name,
            sex:res.data.result.sex,
            from:res.data.result.from
          }
        });
      } else {
        // window.location.replace("/");
        this.$store.commit("saveToken", res.data.result.token);
        console.log(
          "stateArray",
          stateArray,
          "----stateArray[1]",
          stateArray[1],
          "------stateArray[2]",
          stateArray[2]
        );
        if (stateArray && parseInt(stateArray[1]) != 0) {
          this.$router.go(-2);
        } else {
          if (stateArray[2] && stateArray[2] != "0") {
            this.$router.go(-1);
          } else {
            this.$router.replace("/index/home");
          }
        }
      }
    });
  },
  updated() {}, //生命周期 - 更新之后
  destroyed() {} //生命周期 - 销毁完成
};
</script>
<style lang='less' scoped>
//@import url(); 引入公共css类
</style>